<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_News_Controllers_Web extends Gio_Core_Controller
{
    /* ########################### Backend Action ############################*/
	
	public function activateAction()
	{
		$this->setNoRender();
		$this->disableLayout();
		
		$request = $this->getRequest();
		if (!$request->isPost()) {
			return;
		}
		$response = 'RESULT_NOT_OK';
		$webId = $request->getPost('web_id');
		$web = Modules_News_Services_Web::getById($webId);
		
		if (null == $web){
			$this->getResponse()->setBody($response);
			return;	
		}
		
		$act = $request->getPost('act');
		switch ($act) {
			case 'activate':
				$status = 'active';
				break;
			case 'deactivate':
				$status = 'inactive';
				break;
		}
		$web['status'] = $status;
		$response = 'RESULT_OK';
		Modules_News_Services_Web::update($web);
		$this->getResponse()->setBody($response);
	}
	
	public function addAction()
	{
		$request = $this->getRequest();
		
		if ($request->isPost()) {
			$webData = $request->getPost('web');
			$webData = Modules_News_Services_Web::validate($webData);
			
			if (isset($webData['messages_error']) && $webData['messages_error']) {
				$this->view->errorMessages = $webData['messages'];
				$this->view->webData = $webData;
				return;
			}
			
			$web = array(
					'web_id'	   => $webData['web_id'],
					'name' 	   	   => $this->view->STRING->escape($webData['name']),
					'screen_name'  => $this->view->STRING->escape($webData['screen_name']),
					'status'	   => $webData['status'],
			);
			$webId = Modules_News_Services_Web::add($web);
			Gio_Core_Messenger::getInstance()
								->addMessage($this->view->TRANSLATOR->translator('web_actions_add_success'));
			$this->redirect($this->view->url('news_web_add'));
		}
	}
	
	public function deleteAction()
	{
		$this->setNoRender();
		$this->disableLayout();
		
		$request = $this->getRequest();
		if (!$request->isPost()) {
			return;
		}
		$webId = $request->getPost('web_id');
		$web = Modules_News_Services_Web::getById($webId);
		
		$condition = array('web_id' => $webId);
		if (null == $web){
			$this->getResponse()->setBody('RESULT_NOT_OK');
			return;	
		}
		
		/**
		 * Delete web
		 */
		Modules_News_Services_Web::delete($webId);
		$this->getResponse()->setBody('RESULT_OK');
	}
	
	public function editAction()
	{
		$request = $this->getRequest();
		
		$webId = $request->getParam('web_id');
		$web = Modules_News_Services_Web::getById($webId);
		if (null == $web) {
			Modules_Core_Services_Exception::error('DATA_NOT_FOUND');
		}
		
		$this->view->webData = $web;
		
		if ($request->isPost()) {
			$webData = $request->getPost('web');
			$webData = Modules_News_Services_Web::validate($webData);
			
			if (isset($webData['messages_error']) && $webData['messages_error']) {
				$this->view->errorMessages = $webData['messages'];
				$webData['web_id'] = $web['web_id'];
				$this->view->webData = $webData;
				return;
			}
			
			$web['name'] 		 = $this->view->STRING->escape($webData['name']);
			$web['screen_name']  = $this->view->STRING->escape($webData['screen_name']);
			$web['status']	   	 = $webData['status'];
				
			Modules_News_Services_Web::update($web);
			Gio_Core_Messenger::getInstance()
								->addMessage($this->view->TRANSLATOR->translator('web_actions_edit_success'));
			$this->redirect($this->view->url('news_web_edit', $web));
		}
	}
	
	public function listAction()
	{
		$request = $this->getRequest();
		
		$perPage = 20;
		$pageIndex = (int)$request->getParam('page_index');
		if (null == $pageIndex || '' == $pageIndex || $pageIndex < 0) {
			$pageIndex = 1;
		}
		$start = ($pageIndex - 1) * $perPage;
		$this->view->pageIndex = $pageIndex;
		
		$webs = Modules_News_Services_Web::find(array(), $start, $perPage);
		$numWebs = Modules_News_Services_Web::count(array());
		
		$this->view->webs = $webs; 
		
		// Pager
		require_once LIB_DIR . DS . 'PEAR' . DS . 'Pager' . DS . 'Sliding.php';
		$pagerPath = $this->view->url('news_web_list');
		$pagerOptions = array(
			'mode'       	=> 'Sliding',
			'append'   		=> false,
    		'perPage'    	=> $perPage,
    		'delta'      	=> 5,
			'urlVar'		=> 'page',
			'path'			=> $pagerPath,
			'fileName'		=> 'page-%d',
			'separator'		=> '',
			'nextImg' 		=> '<small class="icon arrow_right"></small>',
			'prevImg'		=> '<small class="icon arrow_left"></small>',
			'altNext'		=> '',
			'altPrev'		=> '',
			'altPage'		=> '',
    		'totalItems' 	=> $numWebs,
			'currentPage'	=> $pageIndex,
			'urlSeparator'	=> '/',
			'spacesBeforeSeparator' => 0,
			'spacesAfterSeparator' => 0,
			'curPageSpanPre' => '<a href="javascript: void();" class="current">',
			'curPageSpanPost' => '</a>'
		
		);
		$pager = new Pager_Sliding($pagerOptions);
		$this->view->pager = $pager;
	}
}